import { resolve } from 'path'
import Vue from '@vitejs/plugin-vue'
import Vuejsx from '@vitejs/plugin-vue-jsx'
import ViteLegacy from '@vitejs/plugin-legacy'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { VantResolver } from '@vant/auto-import-resolver'
import { visualizer as Visualizer } from 'rollup-plugin-visualizer'
import { viteVConsole as ViteVConsole } from 'vite-plugin-vconsole'
import ViteRestart from 'vite-plugin-restart'
import ViteImages from 'vite-plugin-vue-images'
import ViteImagemin from 'vite-plugin-imagemin'
import ViteCompression from 'vite-plugin-compression'
import { createHtmlPlugin as CreateHtmlPlugin } from 'vite-plugin-html'

import ExternalGlobals from 'rollup-plugin-external-globals'

export default ({ cwd, notProd: enabled }) => {
  return [
    Vue(),
    Vuejsx(),
    VueSetupExtend(),
    ViteLegacy({
      targets: ['defaults', 'not IE 11'],
    }),
    Visualizer({ open: false }),
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'], // 这些模块里面的api无需引入可直接使用
      dirs: [], // 自定义哪些模块无需导入
      dts: './auto-imports.d.ts', // 无需导入模块的类型文件地址
      resolvers: [VantResolver()],
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: true,
      },
    }),
    Components({
      dirs: [resolve(cwd)],
      dts: true,
      deep: true,
      extensions: ['vue'],
      include: [/\.vue$/, /\.vue\?vue/],
      resolvers: [VantResolver()],
    }),
    ViteRestart({
      restart: ['vite.config.ts'],
    }),
    ViteVConsole({
      entry: resolve(cwd, 'src/main.ts'),
      localEnabled: true,
      enabled,
      config: {
        maxLogNumber: 1000,
        theme: 'dark',
      },
    }),
    ViteImages({
      dirs: ['src/images'], // 图像目录的相对路径
      extensions: ['jpg', 'jpeg', 'png', 'svg', 'webp'], // 有效的图像扩展
      customResolvers: [], // 覆盖名称->图像路径解析的默认行为
      customSearchRegex: '([a-zA-Z0-9]+)', // 重写搜索要替换的变量的Regex。
    }),
    ViteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false,
      },
      optipng: {
        optimizationLevel: 7,
      },
      mozjpeg: {
        quality: 20,
      },
      pngquant: {
        quality: [0.8, 0.9],
        speed: 4,
      },
      svgo: {
        plugins: [
          {
            name: 'removeViewBox',
          },
          {
            name: 'removeEmptyAttrs',
            active: false,
          },
        ],
      },
    }),
    ViteCompression({
      algorithm: 'gzip',
      threshold: 10240,
      verbose: false, // 是否在控制台输出压缩结果
      deleteOriginFile: false,
    }),
    // CreateHtmlPlugin({
    //   minify: true,
    //   inject: {
    //     data: {
    //       VueScript:
    //         '<script src="https://unpkg.com/vue@3.4.21/dist/vue.global.prod.js"></script>',

    //       VueRouterScript:
    //         '<script src="https://unpkg.com/vue-router@4.3.0/dist/vue-router.global.prod.js"></script>',

    //       VueDemiScript:
    //         '<script src="https://unpkg.com/vue-demi@0.14.7/lib/index.iife.js"></script>',

    //       PiniaScript:
    //         '<script src="https://unpkg.com/pinia@2.1.7/dist/pinia.iife.prod.js"></script>',

    //       VueuseCoreScript:
    //         '<script src="https://unpkg.com/@vueuse/core@10.9.0/index.iife.min.js"></script>',
    //       VueuseSharedScript:
    //         '<script src="https://unpkg.com/@vueuse/shared@10.9.0/index.iife.min.js"></script>',

    //       VantScript:
    //         '<script src="https://unpkg.com/vant@4.8.10/lib/vant.min.js"></script>',
    //       VantStyleLink:
    //         '<link rel="stylesheet" href="https://unpkg.com/vant@4.8.10/lib/index.css" />',

    //       AxiosScript:
    //         '<script src="https://unpkg.com/axios@1.6.8/dist/axios.min.js"></script>',

    //       MittScript:
    //         '<script src="https://unpkg.com/mitt@3.0.1/dist/mitt.umd.js"></script>',

    //       NprogressScript:
    //         '<script src="https://unpkg.com/nprogress@0.2.0/nprogress.js"></script>',

    //       NprogressStyleLink:
    //         '<link rel="stylesheet" href="https://unpkg.com/nprogress@0.2.0/nprogress.css" />',

    //       // PiniaPersistScript:
    //       //   '<script src="https://unpkg.com/pinia-plugin-persistedstate@3.2.1/dist/index.mjs"></script>',
    //     },
    //   },
    // }),

    // {
    //   ...ExternalGlobals({
    //     vue: 'Vue',
    //     'vue-demi': 'VueDemi',
    //     '@vueuse/core': 'VueUse',
    //     '@vueuse/shared': 'VueUse',
    //     pinia: 'Pinia',
    //     'vue-router': 'VueRouter',
    //     axios: 'axios',
    //     vant: 'vant',
    //     mitt: 'mitt',
    //     nprogress: 'NProgress',
    //     // 'pinia-plugin-persistedstate': 'pinia-plugin-persistedstate',
    //   }),
    //   enforce: 'post',
    //   apply: 'build',
    // },
  ]
}
